<?php

class Defined_PageBreak{

	public $iCurrentPage = 1;
	public $iPerPage = 20;
	public $iPageRange = 5;
	public $iTotal = 0;
	public $iNumberPage = 11;
	static public $oPager;

	function __construct(){

	}

	function __destruct(){

	}
    
    function wp_page_numbers_check_num($num)
    {
      return ($num%2) ? true : false;
    }
    
    function wp_page_numbers_page_of_page($max_page, $paged, $page_of_page_text, $page_of_of)
    {
    	$pagingString = "";
    	if ( $max_page > 1)
    	{
    		$pagingString .= '<li class="page_info">';
    		if($page_of_page_text == "")
    			$pagingString .= 'Page ';
    		else
    			$pagingString .= $page_of_page_text . ' ';
    		
    		if ( $paged != "" )
    			$pagingString .= $paged;
    		else
    			$pagingString .= 1;
    		
    		if($page_of_of == "")
    			$pagingString .= ' of ';
    		else
    			$pagingString .= ' ' . $page_of_of . ' ';
    		$pagingString .= floor($max_page).'</li>';
    	}
    	return $pagingString;
    }
    
    function wp_page_numbers_prevpage($paged, $max_page, $prevpage)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
        
    	if( $max_page > 1 && $paged > 1 )
    		$pagingString = "<li><a href='$link?page=".($paged-1)."'>$prevpage</a></li>";
    	return $pagingString;
    }
    
    function wp_page_numbers_left_side($max_page, $limit_pages, $paged, $pagingString)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
        
    	$pagingString = "";
    	$page_check_max = false;
    	$page_check_min = false;
    	if($max_page > 1)
    	{
    		for($i=1; $i<($max_page+1); $i++)
    		{
    			if( $i <= $limit_pages )
    			{
    				if ($paged == $i || ($paged == "" && $i == 1))
    					$pagingString .= "<li ><a href='#' class='active'>$i</a></li>";
    				else
    					$pagingString .= "<li><a href='$link?page=$i'>$i</a></li>";
    				if ($i == 1)
    					$page_check_min = true;
    				if ($max_page == $i)
    					$page_check_max = true;
    			}
    		}
    		return array($pagingString, $page_check_max, $page_check_min);
    	}
    }
    
    function wp_page_numbers_middle_side($max_page, $paged, $limit_pages_left, $limit_pages_right)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
    	$pagingString = "";
    	$page_check_max = false;
    	$page_check_min = false;
    	for($i=1; $i<($max_page+1); $i++)
    	{
    		if($paged-$i <= $limit_pages_left && $paged+$limit_pages_right >= $i)
    		{
    			if ($paged == $i)
    				$pagingString .= "<li ><a href='#' class='active'>$i</a></li>";
    			else
    				$pagingString .= "<li ><a href='$link?page=$i'>$i</a></li>";
    				
    			if ($i == 1)
    				$page_check_min = true;
    			if ($max_page == $i)
    				$page_check_max = true;
    		}
    	}
    	return array($pagingString, $page_check_max, $page_check_min);
    }
    
    function wp_page_numbers_right_side($max_page, $limit_pages, $paged, $pagingString)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
        
    	$pagingString = "";
    	$page_check_max = false;
    	$page_check_min = false;
    	for($i=1; $i<($max_page+1); $i++)
    	{
    		if( ($max_page + 1 - $i) <= $limit_pages )
    		{
    			if ($paged == $i)
    				$pagingString .= "<li ><a href='#' class='active'>$i</a.</li>";
    			else
    				$pagingString .= "<li ><a href='$link?page=$i'>$i</a></li>";
    				
    			if ($i == 1)
    			$page_check_min = true;
    		}
    		if ($max_page == $i)
    			$page_check_max = true;
    		
    	}
    	return array($pagingString, $page_check_max, $page_check_min);
    }
    
    function wp_page_numbers_nextpage($paged, $max_page, $nextpage)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
    	if( $paged != "" && $paged < $max_page)
    		$pagingString = "<li ><a href='$link?page=".($paged+1)."'>$nextpage</a></li>";
    	return $pagingString;
    }
    function test(){
        # initialize zend view in manual class
		$oViewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		if ($oViewRenderer->view === null) {
			$oViewRenderer->initView();
		}
		
		 
		$oView= $oViewRenderer->view;
        $sPaging_html=$oView->render('main/paging.phtml');

		return $sPaging_html;
    }
    function wp_page_numbers($max_page, $paged)
    {	
    	if ( !$paged ) { $paged = 1; }	
    
    	$page_of_page = "no";//$config["page_of_page"];
    	$page_of_page_text = "Trang";//$config["page_of_page_text"];
    	$page_of_of = "của tổng";//$config["page_of_of"];
    	
    	$next_prev_text = $config["next_prev_text"];
    	$show_start_end_numbers = $config["show_start_end_numbers"];
    	$show_page_numbers = $config["show_page_numbers"];
    	
    	$limit_pages = "";
    	$nextpage = "►";//$config["nextpage"];<img src='images/iconpageright.png' border='0' />
    	$prevpage = "";//$config["prevpage"];<img src='images/iconpageleft.png' border='0' />
    	$startspace = "...";
    	$endspace = "...";
    	
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
        
    	if( $nextpage == "" ) { $nextpage = "&#9658;"; }
    	if( $prevpage == "" ) { $prevpage = "◄"; }
    	if( $startspace == "" ) { $startspace = "..."; }
    	if( $endspace == "" ) { $endspace = "..."; }
    	
    	if($limit_pages == "") { $limit_pages = "5"; }
    	elseif ( $limit_pages == "0" ) { $limit_pages = $max_page; }
    	
    	if($this->wp_page_numbers_check_num($limit_pages) == true)
    	{
    		$limit_pages_left = ($limit_pages-1)/2;
    		$limit_pages_right = ($limit_pages-1)/2;
    	}
    	else
    	{
    		$limit_pages_left = $limit_pages/2;
    		$limit_pages_right = ($limit_pages/2)-1;
    	}
    	
    	if( $max_page <= $limit_pages ) { $limit_pages = $max_page; }
    	
        
        
    	$pagingString = "<div class='linkPage clearfix'>";
    	$pagingString .= "<ul class='clearfix'>";
    	
    	if($page_of_page != "no")
    		$pagingString .= $this->wp_page_numbers_page_of_page($max_page, $paged, $page_of_page_text, $page_of_of);
    	
    	if( ($paged) <= $limit_pages_left )
    	{
    		list ($value1, $value2, $page_check_min) = $this->wp_page_numbers_left_side($max_page, $limit_pages, $paged, $pagingString);
    		$pagingMiddleString .= $value1;
    	}
    	elseif( ($max_page+1 - $paged) <= $limit_pages_right )
    	{
    		list ($value1, $value2, $page_check_min) = $this->wp_page_numbers_right_side($max_page, $limit_pages, $paged, $pagingString);
    		$pagingMiddleString .= $value1;
    	}
    	else
    	{
    		list ($value1, $value2, $page_check_min) = $this->wp_page_numbers_middle_side($max_page, $paged, $limit_pages_left, $limit_pages_right);
    		$pagingMiddleString .= $value1;
    	}
    	if($next_prev_text != "no")
    		$pagingString .= $this->wp_page_numbers_prevpage($paged, $max_page, $prevpage);
    
    		if ($page_check_min == false && $show_start_end_numbers != "no")
    		{
    			$pagingString .= "<li class='first_last_page' page='1'>";
    			$pagingString .= "<a href='$link?page=1'>1</a>";
    			$pagingString .= "</li><li  class='space'>$startspace</li>\n";
    		}
    	
    	if($show_page_numbers != "no")
    		$pagingString .= $pagingMiddleString;
    	
    		if ($value2 == false && $show_start_end_numbers != "no")
    		{
    			$pagingString .= "<li class='space'>$endspace</li>\n";
    			$pagingString .= "<li class='first_last_page' page='$max_page'>";
    			$pagingString .= "<a href='$link?page=$max_page'>" . $max_page . "</a>";
    			$pagingString .= "</li>";
    		}
    	
    	if($next_prev_text != "no")
    		$pagingString .= $this->wp_page_numbers_nextpage($paged, $max_page, $nextpage);
    	
    	$pagingString .= "</ul>";
    	
    	//$pagingString .= "<div style='float: none; clear: both;'></div>";
    	$pagingString .= "</div>";
    	
    	if($max_page >1)
    		return $pagingString;
    }

	function pager($iTotal = null, $iPerPage = 2, $iCurrPage = 1, $paging_file='main/paging.phtml')
	{
		// if(isset(self::$oPager)) return self::$oPager;

		//       $iPager = Zend_Controller_Front::getInstance()->getRequest()->getParam('page');
		$iPager = ($iCurrPage > 0)?$iCurrPage: 1;

		if ($iPager > 0)
		{
			$this->iCurrentPage = $iPager;
		}

		$this->iTotal = $iTotal;
		$this->iPerPage = $iPerPage;
		$iTotalPage = ceil($this->iTotal / $this->iPerPage);
		// If total page is smaller than called page.
		$iEndPage=$iTotalPage;
		$iStartPage=1;
		if($this->iCurrentPage-$this->iPageRange>$iStartPage)
		{
			$iStartPage = $this->iCurrentPage-$this->iPageRange;
			 

		}
		if($this->iCurrentPage+$this->iPageRange<$iTotalPage)
		{
			$iEndPage = $this->iCurrentPage+$this->iPageRange;
			if($iTotalPage>=$this->iNumberPage){
				if($iEndPage<$this->iNumberPage){
					$iEndPage=$this->iNumberPage;
				}
			}
		}
		if($iTotalPage<=$this->iNumberPage){
			$iEndPage=$iTotalPage;
			$iStartPage=1;
		}

		if ($iTotalPage< $this->iCurrentPage) $this->iCurrentPage=$iTotalPage;
		 
		// Page numnber of core pages.
		// var_dump($this->iPageRange) ;
		// $iCorePage = ceil($this->iCurrentPage / $this->iPageRange);
		 
		// $iStartPage = ($iEndPage - $this->iPageRange) + 1;
		// if($iEndPage>$iTotalPage)
		// $iEndPage=$iTotalPage;
		$arrNavigator=array();
		for ($p = $iStartPage; $p <= $iEndPage; $p++) {
			$arrNavigator[]=$p;
		}
		# initialize zend view in manual class
		$oViewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		if ($oViewRenderer->view === null) {
			$oViewRenderer->initView();
		}
		//  echo 'Total:'.$iTotal;
		/// echo 'iPerPage:'.$iPerPage;
		//  echo 'iCurrPage:'.$iCurrPage;
		// var_dump($arrNavigator);
		 
		$oView= $oViewRenderer->view;
		$oView->pageCount=count($arrNavigator);
		$oView->previous=$iPager-1>0?$iPager-1:1;
		$oView->next=($iPager+1)>$iTotalPage?$iTotalPage:$iPager+1;
		$oView->current=$iPager;
		$oView->pagesInRange=$arrNavigator;
		$sPaging_html=$oView->render($paging_file);

		# Set to static instance
		//self::$oPager = $this;
		return $sPaging_html;
	}
	function pager_popup($iTotal = null, $iPerPage = 2, $iCurrPage = 1)
	{
		// if(isset(self::$oPager)) return self::$oPager;

		$iPager = Zend_Controller_Front::getInstance()->getRequest()->getParam('page');
		$iPager = ($iCurrPage > 0)?$iCurrPage:$iPager;

		if ($iPager > 0)
		{
			$this->iCurrentPage = $iPager;
		}

		$this->iTotal = $iTotal;
		$this->iPerPage = $iPerPage;
		$iTotalPage = ceil($this->iTotal / $this->iPerPage);
		// If total page is smaller than called page.
		$iEndPage=$iTotalPage;
		$iStartPage=1;
		if($this->iCurrentPage-$this->iPageRange>$iStartPage)
		{
			$iStartPage = $this->iCurrentPage-$this->iPageRange;
			 

		}
		if($this->iCurrentPage+$this->iPageRange<$iTotalPage)
		{
			$iEndPage = $this->iCurrentPage+$this->iPageRange;
			if($iTotalPage>=$this->iNumberPage){
				if($iEndPage<$this->iNumberPage){
					$iEndPage=$this->iNumberPage;
				}
			}
		}
		if($iTotalPage<=$this->iNumberPage){
			$iEndPage=$iTotalPage;
			$iStartPage=1;
		}

		if ($iTotalPage< $this->iCurrentPage) $this->iCurrentPage=$iTotalPage;
		 

		$arrNavigator=array();
		for ($p = $iStartPage; $p <= $iEndPage; $p++) {
			$arrNavigator[]=$p;
		}
		# initialize zend view in manual class
		$oViewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
		if ($oViewRenderer->view === null) {
			$oViewRenderer->initView();
		}

		$oView= $oViewRenderer->view;
		$oView->pageCount=count($arrNavigator);
		$oView->previous=$iPager-1>0?$iPager-1:1;
		$oView->next=($iPager+1)>$iTotalPage?$iTotalPage:$iPager+1;
		$oView->current=$iPager;
		$oView->pagesInRange=$arrNavigator;
		$sPaging_html=$oView->render('main/paging_popup.phtml');

		return $sPaging_html;
	}

	function url(array $urlOptions = array(), $name = null, $reset = false)
	{	         
		$obj = new Zend_View_Helper_Url();
		$url = $obj->url($urlOptions, $name, $reset);
		return $url;
	}
    
    function wp_page_numbers_post($max_page, $paged)
    {	
    	if ( !$paged ) { $paged = 1; }	
    
    	$page_of_page = "no";//$config["page_of_page"];
    	$page_of_page_text = "Trang";//$config["page_of_page_text"];
    	$page_of_of = "của tổng";//$config["page_of_of"];
    	
    	$next_prev_text = $config["next_prev_text"];
    	$show_start_end_numbers = $config["show_start_end_numbers"];
    	$show_page_numbers = $config["show_page_numbers"];
    	
    	$limit_pages = "";
    	$nextpage = "►";//$config["nextpage"];<img src='images/iconpageright.png' border='0' />
    	$prevpage = "";//$config["prevpage"];<img src='images/iconpageleft.png' border='0' />
    	$startspace = "...";
    	$endspace = "...";
    	
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
        
    	if( $nextpage == "" ) { $nextpage = "&#9658;"; }
    	if( $prevpage == "" ) { $prevpage = "◄"; }
    	if( $startspace == "" ) { $startspace = "..."; }
    	if( $endspace == "" ) { $endspace = "..."; }
    	
    	if($limit_pages == "") { $limit_pages = "5"; }
    	elseif ( $limit_pages == "0" ) { $limit_pages = $max_page; }
    	
    	if($this->wp_page_numbers_check_num($limit_pages) == true)
    	{
    		$limit_pages_left = ($limit_pages-1)/2;
    		$limit_pages_right = ($limit_pages-1)/2;
    	}
    	else
    	{
    		$limit_pages_left = $limit_pages/2;
    		$limit_pages_right = ($limit_pages/2)-1;
    	}
    	
    	if( $max_page <= $limit_pages ) { $limit_pages = $max_page; }
    	
        
        
    	$pagingString = "<div class='linkPage clearfix'>";
    	$pagingString .= "<ul class='clearfix'>";
    	
    	if($page_of_page != "no")
    		$pagingString .= $this->wp_page_numbers_page_of_page($max_page, $paged, $page_of_page_text, $page_of_of);
    	
    	if( ($paged) <= $limit_pages_left )
    	{
    		list ($value1, $value2, $page_check_min) = $this->wp_page_numbers_left_side_post($max_page, $limit_pages, $paged, $pagingString);
    		$pagingMiddleString .= $value1;
    	}
    	elseif( ($max_page+1 - $paged) <= $limit_pages_right )
    	{
    		list ($value1, $value2, $page_check_min) = $this->wp_page_numbers_right_side_post($max_page, $limit_pages, $paged, $pagingString);
    		$pagingMiddleString .= $value1;
    	}
    	else
    	{
    		list ($value1, $value2, $page_check_min) = $this->wp_page_numbers_middle_side_post($max_page, $paged, $limit_pages_left, $limit_pages_right);
    		$pagingMiddleString .= $value1;
    	}
    	if($next_prev_text != "no")
    		$pagingString .= $this->wp_page_numbers_prevpage_post($paged, $max_page, $prevpage);
    
    		if ($page_check_min == false && $show_start_end_numbers != "no")
    		{
    			$pagingString .= "<li class='first_last_page' page='1'>";
    			//$pagingString .= "<a href='$link?page=1'>1</a>";
                $pagingString .= '<a href="javascript:void(0);" onclick="form_change(1);">1</a>';
    			$pagingString .= "</li><li  class='space'>$startspace</li>\n";   
    		}
    	
    	if($show_page_numbers != "no")
    		$pagingString .= $pagingMiddleString;
    	
    		if ($value2 == false && $show_start_end_numbers != "no")
    		{
    			$pagingString .= "<li class='space'>$endspace</li>\n";
    			$pagingString .= "<li class='first_last_page' page='$max_page'>";
    			//$pagingString .= "<a href='$link?page=$max_page'>" . $max_page . "</a>";
                $pagingString .= '<a href="javascript:void(0);" onclick="form_change('. $max_page .');">'. $max_page .'</a>';
    			$pagingString .= "</li>";
    		}
    	
    	if($next_prev_text != "no")
    		$pagingString .= $this->wp_page_numbers_nextpage_post($paged, $max_page, $nextpage);
    	
    	$pagingString .= "</ul>";
    	
    	//$pagingString .= "<div style='float: none; clear: both;'></div>";
    	$pagingString .= "</div>";
    	
    	if($max_page >1)
    		return $pagingString;
    }
    
    function wp_page_numbers_left_side_post($max_page, $limit_pages, $paged, $pagingString)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
        
    	$pagingString = "";
    	$page_check_max = false;
    	$page_check_min = false;
    	if($max_page > 1)
    	{
    		for($i=1; $i<($max_page+1); $i++)
    		{
    			if( $i <= $limit_pages )
    			{
    				if ($paged == $i || ($paged == "" && $i == 1))
    					$pagingString .= "<li ><a href='#' class='active'>$i</a></li>";
    				else
    					//$pagingString .= "<li><a href='$link?page=$i'>$i</a></li>";
                        $pagingString .= '<li><a href="javascript:void(0);" onclick="form_change('. $i .');">'. $i .'</a></li>';
    				if ($i == 1)
    					$page_check_min = true;
    				if ($max_page == $i)
    					$page_check_max = true;
    			}
    		}
    		return array($pagingString, $page_check_max, $page_check_min);
    	}
    }
    
    function wp_page_numbers_middle_side_post($max_page, $paged, $limit_pages_left, $limit_pages_right)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
    	$pagingString = "";
    	$page_check_max = false;
    	$page_check_min = false;
    	for($i=1; $i<($max_page+1); $i++)
    	{
    		if($paged-$i <= $limit_pages_left && $paged+$limit_pages_right >= $i)
    		{
    			if ($paged == $i)
    				$pagingString .= "<li ><a href='#' class='active'>$i</a></li>";
    			else
    				//$pagingString .= "<li ><a href='$link?page=$i'>$i</a></li>";
    				$pagingString .= '<li><a href="javascript:void(0);" onclick="form_change('. $i .');">'. $i .'</a></li>';
    			if ($i == 1)
    				$page_check_min = true;
    			if ($max_page == $i)
    				$page_check_max = true;
    		}
    	}
    	return array($pagingString, $page_check_max, $page_check_min);
    }
    
    function wp_page_numbers_right_side_post($max_page, $limit_pages, $paged, $pagingString)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
        
    	$pagingString = "";
    	$page_check_max = false;
    	$page_check_min = false;
    	for($i=1; $i<($max_page+1); $i++)
    	{
    		if( ($max_page + 1 - $i) <= $limit_pages )
    		{
    			if ($paged == $i)
    				$pagingString .= "<li ><a href='#' class='active'>$i</a.</li>";
    			else
    				//$pagingString .= "<li ><a href='$link?page=$i'>$i</a></li>";
    				$pagingString .= '<li><a href="javascript:void(0);" onclick="form_change('. $i .');">'. $i .'</a></li>';
    			if ($i == 1)
    			$page_check_min = true;
    		}
    		if ($max_page == $i)
    			$page_check_max = true;
    		
    	}
    	return array($pagingString, $page_check_max, $page_check_min);
    }
    
    function wp_page_numbers_prevpage_post($paged, $max_page, $prevpage)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
        
    	if( $max_page > 1 && $paged > 1 )
    		//$pagingString = "<li><a href='$link?page=".($paged-1)."'>$prevpage</a></li>";
            $pagingString .= '<li><a href="javascript:void(0);" onclick="form_change('. ($paged-1) .');">'. $prevpage .'</a></li>';
    	return $pagingString;
    }
    
    function wp_page_numbers_nextpage_post($paged, $max_page, $nextpage)
    {
        $link = preg_replace('/\?.*/', '', $_SERVER['REQUEST_URI']);
    	if( $paged != "" && $paged < $max_page)
    		//$pagingString = "<li ><a href='$link?page=".($paged+1)."'>$nextpage</a></li>";
            $pagingString .= '<li><a href="javascript:void(0);" onclick="form_change('. ($paged+1) .');">'. $nextpage .'</a></li>';
    	return $pagingString;
    }
    
}


?>